package cn.aopmin.test;

import cn.aopmin.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author aopmin
 * @since 2024/12/8
 */
public class DemoTest01 {
      public static void main(String[] args) throws IOException {
        // 1. 读取核心配置文件 --- 有没有加载数据源
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);

        // 2. 创建sqlsession工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        // 3. 创建sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 4. 执行sql语句 , 参数映射 结果封装
         User user = sqlSession.selectOne("cn.aopmin.dao.UserMapper.findById", 1);
        System.out.println(user);

        // 5. 释放资源
        sqlSession.close();
        is.close();
      }
}
